package com.caipiao.lottery.lty.client;

import java.net.URISyntaxException;

import com.caipiao.lottery.core.ErrorDef;
import com.caipiao.lottery.log.TraceLogger;
import com.caipiao.lottery.lty.message.LtyMsgBodyFactory;
import com.caipiao.lottery.lty.message.LtyRequest;
import com.caipiao.lottery.lty.message.LtyResponse;
import com.caipiao.lottery.lty.message.MessageCodeHelper;
import com.caipiao.lottery.lty.message.MessageResponse;
import com.caipiao.util.StringTools;

import jodd.http.HttpRequest;
import jodd.http.HttpResponse;


public class LtyHttpClient
{	
	final String url;
	
	public LtyHttpClient(String remoteURL) throws URISyntaxException
	{
		this.url = remoteURL;
	}
	
	//发送消息
	public void Cmd_Post(LtyRequest req, LtyResponse resp, String aeskey) throws Exception
	{
		String url = this.url + req.productId + "/" + req.channelId + "/" + req.cmd + "/" + req.version + "/";
		
		String interLog = req.msgId + " - LtyClnt请求接口:" + req.cmd;
		
		if (null == this.url || this.url.isEmpty())
		{
			TraceLogger.Lty.error(interLog + " URL配置错误");
			throw new Exception("URL配置错误");
		}
		
		//构建请求包体数据
		String req_ming_body = "";
		if (null != req.bodyMsg)
		{
			req_ming_body = req.bodyMsg.ToString();
			
			//加密数据
			req.body = MessageCodeHelper.EncodeBody(req.msgId, 
	                                                            						req.mode, 
	                                                            						req_ming_body, 
	                                                            						aeskey, 
	                                                            						TraceLogger.LtyBin);
			
			
			//构建摘要
			req.digest = StringTools.MD5EncodeToHex(req.body);
		}
		
		TraceLogger.LtyBin.info(interLog + " - 请求包体:\n" + req_ming_body);
		
		if (null != req.sessionData)
		{
			String sign = StringTools.MD5EncodeToHex(req.cmd + req.deviceId + req.sessionData.sid + req.sessionData.skey + req.digest);
			req.session  = req.sessionData.sid + "$" + sign;
		}

	     //设置数据
	     String reqStr = req.ToString();
	     
	     TraceLogger.LtyBin.info(interLog + " \n Post URL:" + url + " \n Send:" + reqStr);
		
	     HttpRequest http_req = HttpRequest.post(url);
		
		http_req.body(reqStr);
		
		HttpResponse http_resp = http_req.send();

	     //通信完成处理缓冲区数据
	     String resp_Str = http_resp.body();
	     
	     TraceLogger.LtyBin.info(interLog +" \n Post URL:" + url + " \n Receive:" + resp_Str);
	     
	     //缺省错误
	     resp.result = ErrorDef.SYS_BUSY;
	     
	     LtyResponse resp_ = (LtyResponse) resp.ParseFromString(resp_Str);
	     
	     //解析响应数据
	     resp.body = resp_.body;
	     resp.digest = resp_.digest;
	     resp.mode = resp_.mode;
	     resp.result = resp_.result;
	     
	     //解密响应数据
	     String ming_resp_body = MessageCodeHelper.DecodeBody(req.msgId,
		    		 																			resp.mode,
		    		 																			resp.body,
		    		 																			aeskey,
		    		 																			TraceLogger.LtyBin);
	     
	     TraceLogger.LtyBin.info(interLog + " - 响应包体:\n" + ming_resp_body);
	     
	     //获取响应解析包体
	     resp.bodyMsg = LtyMsgBodyFactory.GetLtyRespBody(req.cmd);
	     if (resp.result.equalsIgnoreCase("0"))
	     {
	    	 if (null == resp.bodyMsg)
	    	 {
	    		 throw new Exception("未实现的响应包体:" + req.cmd);
	    	 }
	    	 
	    	 //解析包体
	    	 resp.bodyMsg = (MessageResponse)resp.bodyMsg.ParseFromString(ming_resp_body);
	     }
	     
	     //结束
	}
}
